import time

from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By

from common.Utils import TestDriver


# 测试问卷考试系统我的题库页面
class QuestionTest:
    driver = ""
    url = ""

    def __init__(self):
        self.driver = TestDriver.driver
        self.url = "http://8.155.1.153:8081/repo/index"
        self.driver.get(self.url)
        self.driver.maximize_window()

    # 进行页面元素的查找检查
    def QuestionCheckElement(self):
        # 检查名称输入标签
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(1) > div > div > div.ant-col.ant-form-item-label > label")
        # 检查名称输入框
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(1) > div > div > div.ant-col.ant-form-item-control > div > div > span")
        # 检查类型选择框标签
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(2)")
        # 检查重置按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(1) > button")
        # 检查查询按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(2) > button")
        # 检查新建按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div:nth-child(1) > button")
        # 检查导出按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div:nth-child(2) > button")
        # 检查题库列表
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-pro-table-list-toolbar-left > div")
        # 检查文本导入按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > div > div > div > div > div > table > tbody > tr:nth-child(2) > td:nth-child(9) > div > div:nth-child(1) > a")
        # 检查试题管理按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > div > div > div > div > div > table > tbody > tr:nth-child(2) > td:nth-child(9) > div > div:nth-child(3) > a")

        # 添加屏幕截图
        TestDriver.getScreenshot()

    # 题库相关功能测试
    def QuestionFuncTest(self):
        # 新建题库测试
        actions = ActionChains(self.driver)
        # 点击新建
        TestDriver.getScreenshot()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div:nth-child(1) > button").click()
        # 新建弹出的页面的元素名和原先页面的部分元素名重叠，要限制操作页面的范围
        newspan_element = self.driver.find_element(By.CSS_SELECTOR,
                                                   "body > div:nth-child(7) > div > div.ant-drawer-content-wrapper > div > div > div.ant-drawer-body")

        # 填写信息
        newspan_element.find_element(By.CSS_SELECTOR, "#name").send_keys("test")
        # 选择类型
        newspan_element.find_element(By.CSS_SELECTOR,
                                     "body > div:nth-child(7) > div > div.ant-drawer-content-wrapper > div > div > div.ant-drawer-body > form > div:nth-child(2) > div > div:nth-child(4) > div > div.ant-row.ant-form-item-row > div.ant-col.ant-form-item-control > div.ant-form-item-control-input > div > div > div.ant-select-selector").click()

        # actions.click(option).perform()
        actions.key_down(Keys.ARROW_DOWN).key_down(Keys.ENTER).perform()
        TestDriver.getScreenshot()
        time.sleep(1)

        # 点击确定
        newspan_element.find_element(By.XPATH,
                                     "/html/body/div[4]/div/div[3]/div/div/div[3]/div/div/div[2]/button").click()
        TestDriver.getScreenshot()

        ##########################################################################################################################
        # 测试查询功能并测试能否查询到刚刚创建的考卷

        # 输入查询名称
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("test")
        # 选择类型
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(2) > div > div > div.ant-col.ant-form-item-control > div > div > div > div").click()
        time.sleep(1)
        actions.key_down(Keys.ARROW_DOWN).key_down(Keys.ENTER).perform()
        # 点击查询
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(2) > button").click()
        # 检测是否查询到新建的问卷
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > div > div > div > div > div > table > tbody > tr:nth-child(2) > td:nth-child(1)")
        TestDriver.getScreenshot()

        ################################################################################################################################
        # 测试重置
        # 点击重置

        TestDriver.getScreenshot()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(1) > button").click()
        # 检查输入框内是否清空搜索信息，清空则为重置到初始状态
        assert self.driver.find_element(By.CSS_SELECTOR, "#name").get_attribute("value") == ""

        TestDriver.getScreenshot()


